package com.example.accessdemo.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.example.accessdemo.domain.CustomPage;
import com.example.accessdemo.domain.Result;
import com.example.accessdemo.domain.SysDept;
import com.example.accessdemo.domain.SysNotice;
import com.example.accessdemo.service.SysDeptService;
import com.example.accessdemo.service.SysNoticeService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.time.Duration;
import java.util.ArrayList;
@RestController
public class SseController {
    @Autowired
    private SysNoticeService sysNoticeService;
    
    @Autowired
    private SysDeptService sysDeptService;

    @GetMapping(value = "/sse", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> getSseStream() {
        // 使用Flux生成每秒一个递增的数据流，用于模拟实时数据推送
        return Flux.interval(Duration.ofSeconds(1))
                .map(sequence -> "Data: " + sequence);
    }
    
    @GetMapping(value = "/notice")
    public Result getNotice( @ApiParam("当前页") @RequestParam(value = "page", defaultValue = "1") Integer page,
                             @ApiParam("页面大小") @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize){
        CustomPage<SysNotice> customPage = new CustomPage<>(page,pageSize);
        
        /**
         * 基于status字段排序
         */
        ArrayList<OrderItem> orderItems = new ArrayList<>();
        OrderItem orderItem = new OrderItem("status",false);
        orderItems.add(orderItem);
        customPage.setOrders(orderItems);
        
        CustomPage<SysNotice> sysNoticeCustomPage = sysNoticeService.selectData(customPage);
        return Result.success(sysNoticeCustomPage);
    }
    
    @GetMapping(value = "/getFullName")
    public Result getFullName(String id){
        
        String result = "";
        SysDept byId = sysDeptService.getById(108);
        if(ObjectUtil.isNotNull(byId)){
            result = sysDeptService.buildFullName(String.valueOf(byId.getDept_id()));
        }
        
        return Result.success(result);
    }
}
